package com.shd.edanpei.customer.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.bean.StatusBean;
import com.shd.edanpei.customer.bean.PayInfoModel;
import com.shd.edanpei.customer.contract.PayContract;
import com.shd.edanpei.customer.presenter.PayPresenter;
import com.shd.edanpei.enums.OrderTypeEnum;
import com.shd.edanpei.enums.PaymentEnum;
import com.shd.edanpei.enums.StatusEnum;
import com.shd.edanpei.util.ToastUtil;
import com.shd.edanpei.util.pay.PayUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/4/4.
 * 选择支付
 */

public class PayActivity extends BaseActivity<PayPresenter> implements PayContract.PayIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.iv_type)
    ImageView ivType;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.tv_pay)
    TextView tvPay;
    @BindView(R.id.rg_payment)
    RadioGroup rgPayment;
    @BindView(R.id.rb_payment_alipay)
    RadioButton rbAlipay;
    @BindView(R.id.view_payment_point_layer)
    View pointLayer;

    private int mIndex;
    private int mOrderId;

    @Override
    protected int setLayout() {
        return R.layout.activity_payment;
    }

    @Override
    protected PayPresenter bindPresenter() {
        return new PayPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);
        headline.setText("选择支付");
        Bundle extras = getIntent().getExtras();
        mIndex = extras.getInt(Constant.INDEX);
        mOrderId = extras.getInt(Constant.ORDER_ID);
        mPresenter.getData(mOrderId);

    }

    @Override
    protected void initListener() {

    }

    @Override
    public void setData(PayInfoModel model) {
        ivType.setImageDrawable(ContextCompat.getDrawable(this, OrderTypeEnum.getImg(model.getOrderType())));
        tvType.setText(OrderTypeEnum.getValue(model.getOrderType()));
        tvAmount.setText(model.getLoanAmount());
        tvPay.setText(model.getPayAmount());

    }

    @OnClick(R.id.iv_headline_back)
    public void onViewClicked() {
        finish();
    }

    @OnClick(R.id.tv_pay_sure)
    void paySure() {
        PaymentEnum paymentEnum = null;
        switch (rgPayment.getCheckedRadioButtonId()) {
            case R.id.rb_payment_alipay:
                paymentEnum = PaymentEnum.alipay;
                break;
            case R.id.rb_payment_wechat:
                paymentEnum = PaymentEnum.wechat;
                 break;
        }
        mPresenter.payDeposit(mOrderId, paymentEnum, mIndex);
    }

    private void setData(int viewId, String data) {
        TextView view = findViewById(viewId);
        view.setText(data);
    }


    @Subscribe(code = RxBusCode.WECHAT_PAY_SUCCESS)
    public void weChatPaySuccess() {
        RxBus.get().send(RxBusCode.REFRESH_ORDER_STATUS, new StatusBean(mIndex, StatusEnum.applying.getStatus()));
        finish();

    }

    @Subscribe(code = RxBusCode.WECHAT_PAY_ERROR)
    public void weChatPayError() {
        ToastUtil.showToast(PayUtils.PAY_FAILED);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.get().unRegister(this);
    }
}
